package com.chendx.store.order.mq.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chendx.model.order.pojo.Order;
import com.chendx.model.order.pojo.OrderItem;
import com.chendx.model.order.vo.OrderNotice;
import com.chendx.store.common.constants.ampq.AMQPConstants;
import com.chendx.store.common.constants.order.OrderConstants;
import com.chendx.store.common.entity.IdWorker;
import com.chendx.store.order.mapper.OrderItemMapper;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by chendx on 2022/4/24.
 */
@Component
@RabbitListener(queues = "order_timer_notice_queue")
public class OrderPayDelayNoticeMessageListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private Environment env;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @RabbitHandler
    public void getOrderPayDelayNoticeMessage(String message){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("监听消息 " + simpleDateFormat.format(new Date()));
        String[] messages = message.split(",");
        String type = messages[0];
        if (type.equals("order")){
            System.out.println("普通订单消息处理 " + messages[1]);
            Object orderObj = redisTemplate.boundHashOps(OrderConstants.ORDERLIST).get(messages[1]);
            if (orderObj == null){
                return;
            }
            Order order = JSON.parseObject(orderObj.toString(),Order.class);
            if (order != null && order.getPayStatus().equals("0")){
                List<OrderItem> orderItems = orderItemMapper.selectList(Wrappers.<OrderItem>lambdaQuery().eq(OrderItem::getOrderId, order.getId()));
                if (orderItems == null || orderItems.size() < 1){
                    return;
                }
                String image = orderItems.get(0).getImage();
                RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate.getConnectionFactory());
                Queue queue = new Queue(env.getProperty(AMQPConstants.MQ_ORDER_QUEUE_ORDERPQYNOTICE) + order.getUsername());
                rabbitAdmin.declareQueue(queue);
                DirectExchange exchange = new DirectExchange(env.getProperty(AMQPConstants.MQ_ORDER_EXCHANGE_ORDERPAYNOTICE));
                rabbitAdmin.declareExchange(exchange);
                rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(order.getUsername()));

                OrderNotice orderNotice = new OrderNotice();
                String id = idWorker.nextId()+"";
                orderNotice.setId(id);
                orderNotice.setOrderId(order.getId());
                orderNotice.setState("0");
                orderNotice.setUsername(order.getUsername());
                orderNotice.setTitle("订单未支付通知");
                orderNotice.setCreateTime(new Date());
                orderNotice.setOrderType("order");
                orderNotice.setOrderImg(image);
                orderNotice.setContent("订单未支付，请及时支付。");

                redisTemplate.boundHashOps(OrderConstants.ORDER_NOTICE_USER + order.getUsername()).put(order.getId(),JSON.toJSONString(orderNotice));
                rabbitTemplate.convertAndSend(env.getProperty(AMQPConstants.MQ_ORDER_EXCHANGE_ORDERPAYNOTICE),order.getUsername(), JSON.toJSONString(order));
            }
        }
    }
}
